<?php

/**
 * Administrator module class file.
 *
 * @author Tu Hoang Anh <tuha@k57c.com>
 * @copyright Copyright &copy; 2012 Tu Hoang Anh
 * @version 0.0.1
 *
 * DO NOT CHANGE THE DEFAULT CONFIGURATION VALUES!
 *
 * You may overload the module configuration values in your administrator-module
 * configuration like so:
 *
 * 'modules'=>array(
 *     'administrator'=>array(
 *         'baseUrl'=>'/administrator'
 *     ),
 * ),
 */
class AdministratorModule extends CWebModule {

    /**
     * @property boolean whether to install administrator when accessed.
     */
    public $install = false;

    /**
     * @property string the base url to administrator. Override when module is nested.
     */
    public $baseUrl = '/administrator';

    /**
     * @property string the path to the layout file to use for displaying administrator.
     */
    public $layout = 'administrator.views.layouts.main';

    /**
     * @property string the path to the application layout file.
     */
    public $appLayout = 'application.views.layouts.main';

    /**
     * @property boolean whether to enable debug mode.
     */
    public $debug = false;
    private $_assetsUrl;

    /**
     * Initializes the "administrator" module.
     */
    public function init() {
        // Set required classes for import.
        $this->setImport(array(
            'administrator.components.*',
            'administrator.controllers.*',
            'administrator.models.*',
        ));

        // Set the required components.
        Yii::app()->user->loginUrl = '/administrator/default/login';
        Yii::app()->homeUrl = '/administrator';

        // Normally the default controller is default.
        $this->defaultController = 'default';
    }

    /**
     * Registers the necessary scripts.
     */
    public function registerScripts() {
        // Get the url to the module assets
        $assetsUrl = $this->getAssetsUrl();

        // Register the necessary scripts
        $cs = Yii::app()->getClientScript();
        $cs->registerCssFile($assetsUrl . '/css/global.css')
                ->registerCssFile($assetsUrl . '/css/layout.css')
                ->registerCssFile($assetsUrl . '/css/index.css');
    }

    /**
     * Publishes the module assets path.
     * @return string the base URL that contains all published asset files of Rights.
     */
    public function getAssetsUrl() {
        if ($this->_assetsUrl === null) {
            $assetsPath = Yii::getPathOfAlias('administrator.assets');

            // We need to republish the assets if debug mode is enabled.
            if ($this->debug === true)
                $this->_assetsUrl = Yii::app()->getAssetManager()->publish($assetsPath, false, -1, true);
            else
                $this->_assetsUrl = Yii::app()->getAssetManager()->publish($assetsPath);
        }

        return $this->_assetsUrl;
    }

    /**
     * @return the current version.
     */
    public function getVersion() {
        return '0.0.1';
    }

}